<?php
interface Observer{
	function notify($obj);
}

class ExchangeRate{
	private static $instance = null;
	private $observers = array();
	private $exchage_rate;

	private function __construct(){}
	
	public function getExchageRate(){
		return $this->exchage_rate;
	}
	
	public function  registerObserver($obj){
		$this->observers[] = $obj;
	}
	
	public static function getInstance(){
		if(is_null(self::$instance)){
			self::$instance = new ExchangeRate();
		}
		return self::$instance;
	}
	
	public function setExchangeRate($new_rate){
		$this->exchage_rate = $new_rate;
		$this->nofityObservers();
	}
	
	public function nofityObservers(){
		foreach($this->observers as $obj){
			$obj->notify($this);
		}
	}
}

class ProductItem implements Observer{
	public  function __construct(){
		ExchangeRate::getInstance()->registerObserver($this);
	}
	
	public function notify($obj){
		if($obj instanceof ExchangeRate){
			//update exchage rate
			$newRate = $obj->getExchageRate();
			print "Received updated! the new rate is: $newRate<br />";
		}
	}
}

$pro1 = new ProductItem();
$pro2 = new ProductItem();
ExchangeRate::getInstance()->setExchangeRate(4.5);